import { myRequest } from '../../api/myRequest'
import { actionsMap } from '../../api/url'

const $RC = 'Mesh'
const $RC_actions = actionsMap[$RC]
const $RC_DownloadFile = 'MeshDownloadFile'
const $RC_DownloadFile_actions = actionsMap[$RC_DownloadFile]

export default {
  Mesh: {
    /**
     * 查询列表
     * @param recipient
     * @returns {Promise<*>}
     */
    query(filter) {
      let recipient = [{ action: $RC_actions.QUERY, ...filter }]
      return myRequest($RC, recipient)
    },
    queryByName(name) {
      let recipient = [{ action: $RC_actions.QUERY_BY_NAME, name}]
      return myRequest($RC, recipient)
    },
    queryByNames(names) {
      let recipient = [{ action: $RC_actions.QUERY_BY_NAMES, names }]
      return myRequest($RC, recipient)
    },
    queryInfo(filter={}) {
      let recipient = [{ action: $RC_actions.QUERY_INFO, ...filter }]
      return myRequest($RC, recipient)
    },
    queryByIds(ids) {
      let recipient = [{ action: $RC_actions.QUERY_BY_IDS, ids }]
      return myRequest($RC, recipient)
    },
    queryByPage(pageNumber, pageSize, filter) {
      return myRequest($RC, [
        { action: $RC_actions.QUERY_BY_PAGE, pageNumber: pageNumber, pageSize: pageSize, ...filter },
      ])
    },
    queryInfoByPage(pageNumber, pageSize, filter) {
      return myRequest($RC, [
        { action: $RC_actions.QUERY_INFO_BY_PAGE, pageNumber: pageNumber, pageSize: pageSize, ...filter },
      ])
    },
    queryInfoAll() {
      let recipient = [{ action: $RC_actions.QUERY_INFO_ALL }]
      return myRequest($RC, recipient)
    },
    // ******************************模型配置方法开始*****************************
    /**
     * 添加信息
     * @param record
     * @returns {Promise<*>}
     */
    add(record) {
      return myRequest($RC, [{ action: $RC_actions.ADD, data: [record] }])
    },
    /**
     * 更新信息
     * @param record
     * @returns {Promise<*>}
     */
    update(record) {
      return myRequest($RC, [{ action: $RC_actions.UPDATE, data: [record] }])
    },
    /**
     * 批量删除信息
     */
    delete(filter={}) {
      return myRequest($RC, [{ action: $RC_actions.DELETE, ...filter }])
    },
    /**
     * 批量删除信息
     * @param ids
     * @returns {Promise<*>}
     */
    deleteByIds(ids) {
      return myRequest($RC, [{ action: $RC_actions.DELETE_BY_IDS, ids }])
    },
    // 下载
    downloadFileById(id) {
      return myRequest($RC_DownloadFile, [{ action: $RC_DownloadFile_actions.DOWNLOAD_FILE_BY_ID, id }])
    },
    downloadFileByName(name) {
      return myRequest($RC_DownloadFile, [{ action: $RC_DownloadFile_actions.DOWNLOAD_FILE_BY_NAME, name }])
    },
    downloadFileBase64ById(id) {
      return myRequest($RC, [{ action: $RC_actions.DOWNLOAD_FILE_BASE64_BY_ID, id }])
    },
    downloadFileBase64ByName(name) {
      return myRequest($RC, [{ action: $RC_actions.DOWNLOAD_FILE_BASE64_BY_NAME, name }])
    },
    // 删除文件
    deleteFileById(id) {
      return myRequest($RC, [{ action: $RC_actions.DELETE_FILE_BY_ID, id }])
    },
    deleteFileByName(name) {
      return myRequest($RC, [{ action: $RC_actions.DELETE_FILE_BY_NAME, name }])
    },
    uploadFile(file, id) {
      return myRequest(actionsMap.MeshUploadFile, {file, id} )
    },
  },
}
